<template> 
	<div class="JNPF-common-layout">
		<div class="JNPF-common-layout-center">
			<el-row class="JNPF-common-search-box" :gutter="16">
				<el-form @submit.native.prevent>
					<el-col :span="6">
						<el-form-item label="安全制度名称">
							<el-input v-model="query.aqzdmc" placeholder="安全制度名称" clearable  />
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item label="发布日期">
							<el-date-picker v-model="query.fbrq" placeholder="发布日期" clearable format="yyyy-MM-dd" type="date"  />
						</el-form-item>
					</el-col>
                    <el-col :span="6">
                        <el-form-item label="分类">
                            <el-select v-model="query.axfl" placeholder="请选择" clearable :style='{"width":"100%"}'>
                                <el-option v-for="(item, index) in axflOptions" :key="index" :label="item.fullName" :value="item.id" ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
					<el-col :span="6">
					<el-form-item>
						<el-button type="primary" icon="el-icon-search" @click="search()">查询</el-button>
						<el-button icon="el-icon-refresh-right" @click="reset()">重置</el-button>
					</el-form-item>
					</el-col>
				</el-form>
			</el-row>
			<div class="JNPF-common-layout-main JNPF-flex-main">
				<div class="JNPF-common-head">
					<div>
						<el-button type="primary" icon="el-icon-plus" @click="addOrUpdateHandle()">
							新增
						</el-button>
						<el-button type="text" icon="el-icon-download" @click="exportData()">
							导出
						</el-button>
					</div>
					<div class="JNPF-common-head-right">
						<el-tooltip effect="dark" content="刷新" placement="top">
							<el-link icon="icon-ym icon-ym-Refresh JNPF-common-head-icon" :underline="false"  @click="reset()" />
						</el-tooltip>
						<screenfull :isContainer="true" />
						<el-tooltip effect="dark" content="列设置" placement="top">
							<el-link icon="icon-ym icon-ym-options JNPF-common-head-icon" :underline="false" />
						</el-tooltip>
					</div>
				</div>
				<JNPF-table v-loading="listLoading" :data="list">
					<el-table-column prop="aqzdmc" label="安全制度名称" align="left" />
					<el-table-column label="发布部门"  prop="fbbm" algin="left">
						<template slot-scope="scope">{{ scope.row.fbbm | dynamicText(fbbmOptions) }}</template>
					</el-table-column>
					<el-table-column prop="fbrq" label="发布日期" align="left" />
					<el-table-column label="适用部门"  prop="sybm" algin="left">
						<template slot-scope="scope">{{ scope.row.sybm | dynamicText(sybmOptions) }}</template>
					</el-table-column>
					<el-table-column label="评审人"  prop="psr" algin="left">
						<template slot-scope="scope">{{ scope.row.psr | dynamicText(psrOptions) }}</template>
					</el-table-column>
					<el-table-column prop="psrq" label="评审日期" align="left" />
					<el-table-column label="是否现行"  prop="sfxx" algin="left">
						<template slot-scope="scope">{{ scope.row.sfxx | dynamicText(sfxxOptions) }}</template>
					</el-table-column>
                    <el-table-column label="分类"  prop="axfl" algin="left" />
					<el-table-column label="操作" fixed="right" width="150">
						<template slot-scope="scope">
							<el-button type="text" v-if="scope.row.sfxx==1" @click="shBtnHandle(scope.row.id)" >
								审核
							</el-button>
                            <el-button type="text" @click="addOrUpdateHandle(scope.row.id)" >
								编辑
							</el-button>
							<el-button type="text" @click="addOrUpdateHandle(scope.row.id,true)" >
								详情
							</el-button>
							<el-button type="text" @click="handleDel(scope.row.id)" class="JNPF-table-delBtn">
								删除
							</el-button>
						</template>
					</el-table-column>
				</JNPF-table>
				<pagination :total="total" :page.sync="listQuery.currentPage" :limit.sync="listQuery.pageSize" @pagination="initData" />
			</div>
		</div>
		<JNPF-Form v-if="formVisible" ref="JNPFForm" @refresh="refresh" />
		<ExportBox v-if="exportBoxVisible" ref="ExportBox" @download="download" />
		<flow v-show="flowVisible1" ref="flow" @close="flowVisible1=false" @chioceFlow="chioceFlow" />
		<component :is="currentView" v-if="formVisible1" @close="colseForm" ref="JNPFForm1" />  
	</div>

</template> 
<script>
    import request from '@/utils/request'
    import {getDictionaryDataSelector} from '@/api/systemData/dictionary'
    import JNPFForm from './Form'
    import ExportBox from './ExportBox'
    import {previewDataInterface} from '@/api/systemData/dataInterface'
	import { FlowEngineListAll } from '@/api/workFlow/FlowEngine'
    import edit from '@/views/workFlow/fromBox/Edit'
    import audit from '@/views/workFlow/fromBox/Audit'
    import flow from './Flow'

    export default {
        components: {JNPFForm, ExportBox, FlowEngineListAll, edit, audit, flow},
        // components: {JNPFForm, ExportBox},
        data() {
            return {
                showAll: false,
                query: {
					aqzdmc:undefined,
					fbrq:undefined,
					fl:undefined,

                },
                defaultProps: {
                    children: 'children',
                    label: 'fullName'
                },
                list: [],
                listLoading: true,
                total: 0,
                listQuery: {
					currentPage: 1,
					pageSize: 20,

                    sort: "desc",
                    sidx: "",
                },
                formVisible: false,
                exportBoxVisible: false,
                columnList: [
					{prop: 'aqzdmc', label: '安全制度名称'},
					{prop: 'fbbm', label: '发布部门'},
					{prop: 'fbrq', label: '发布日期'},
					{prop: 'sybm', label: '适用部门'},
					{prop: 'psr', label: '评审人'},
					{prop: 'psrq', label: '评审日期'},
					{prop: 'sfxx', label: '是否现行'},

                ],
				fbbmOptions:[],
				sfxxOptions:[{"fullName":"是","id":"0"},{"fullName":"否","id":"1"}],
				sybmOptions:[],
				txrOptions:[],
				txrbmOptions:[],
				psrOptions:[],
				cpryOptions:[],

				flowEngineListAll: [],
				flowVisible1: false,
				formVisible1: false,
				currentView: 'edit',
                axflOptions:[{"fullName":"法律法规","id":"法律法规"},{"fullName":"体系文件","id":"体系文件"},{"fullName":"规章制度","id":"规章制度"},{"fullName":"作业指导书","id":"作业指导书"}],
            }
        },
        computed: {},
        created() {
			this.initData();
			this.getfbbmOptions();
			this.getsybmOptions();
			this.gettxrOptions();
			this.gettxrbmOptions();
			this.getpsrOptions();
			this.getcpryOptions();

			this.getDictionaryData();
        },
        methods: {
			getfbbmOptions()
			{
				previewDataInterface('772d2cbcadd24df79243e3d92ef5f690').then(res => {
					this.fbbmOptions = res.data
				})
			},
			getsybmOptions()
			{
				previewDataInterface('772d2cbcadd24df79243e3d92ef5f690').then(res => {
					this.sybmOptions = res.data
				})
			},
			gettxrOptions()
			{
				previewDataInterface('8bca424f16d6448ab09e57a028853aa9').then(res => {
					this.txrOptions = res.data
				})
			},
			gettxrbmOptions()
			{
				previewDataInterface('772d2cbcadd24df79243e3d92ef5f690').then(res => {
					this.txrbmOptions = res.data
				})
			},
			getpsrOptions()
			{
				previewDataInterface('8bca424f16d6448ab09e57a028853aa9').then(res => {
					this.psrOptions = res.data
				})
			},
			getcpryOptions()
			{
				previewDataInterface('8bca424f16d6448ab09e57a028853aa9').then(res => {
					this.cpryOptions = res.data
				})
			},


            initData() {
                this.listLoading = true;
                let query = {
                    ...this.listQuery,
                    ...this.query
                };
                request({
                    url: `/api/System/qyscAqgzzd`,
                    method: 'get',
                    data: query
                }).then(res => {
                    this.list = res.data.list
                    this.total = res.data.pagination.total
                    this.listLoading = false
                    this.$nextTick(() => {
                        this.tableHeight = "100%"
                    });
                })
            },
            handleDel(id) {
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    type: 'warning'
                }).then(() => {
                    request({
                        url: `/api/System/qyscAqgzzd/${id}`,
                        method: 'DELETE'
                    }).then(res => {
                        this.$message({
                            type: 'success',
                            message: res.msg,
                            onClose: () => {
                                this.initData()
                            }
                        });
                    })
                }).catch(() => {
                });
            },
            addOrUpdateHandle(id,isDetail) {
                this.formVisible = true
                this.$nextTick(() => {
                    this.$refs.JNPFForm.init(id,isDetail)
                })
            },
            exportData() {
                this.exportBoxVisible = true
                this.$nextTick(() => {
                    this.$refs.ExportBox.init(this.columnList)
                })
            },
            download(data) {
                let query = {...data, ...this.listQuery, ...this.query}
                request({
                    url: `/api/System/qyscAqgzzd/Actions/Export`,
                    method: 'POST',
                    data: query
                }).then(res => {
                    if (!res.data.url) return
                    window.location.href = this.define.comUrl + res.data.url
                    this.$refs.ExportBox.visible = false
                    this.exportBoxVisible = false
                })
            },
            search() {
                this.listQuery = {
					currentPage: 1,
					pageSize: 20,

                    sort: "desc",
                    sidx: "",
                }
                this.initData()
            },
            refresh(isrRefresh) {
                this.formVisible = false
                if (isrRefresh) this.reset()
            },
            reset() {
                for (let key in this.query) {
                    this.query[key] = undefined
                }
                this.listQuery = {
					currentPage: 1,
					pageSize: 20,

                    sort: "desc",
                    sidx: "",
                }
                this.initData()
            },
			shBtnHandle(id){
				this.flowVisible1 = true
                this.$nextTick(() => {
                    let flowAllList = this.flowEngineListAll;
                    if(flowAllList.length > 0){
                        for(let i=0;i<flowAllList.length;i++){
                            if(flowAllList[i].fullName == "安全管理"){
                                if(flowAllList[i].children.length > 0){
                                    let childList = flowAllList[i].children;
                                    for(let r=0;r<childList.length;r++){
										if(childList[r].id == "c420d4b7410b4458bde03c3c46a9272c"){  //安全制度临时表
											this.$refs.flow.init1(this.flowEngineListAll,id)
										}
                                    }
                                }
                            }
                        }
                    }
                })
			},
			chioceFlow(item,mainId) {
                let data = {
                    enCode: item.enCode,
                    flowId: item.id,
                    formType: item.formType,
                    id: '',
                    mainid: mainId
                }
                this.currentView = 'edit'
                this.$nextTick(() => {
                    this.formVisible1 = true
                    this.$nextTick(() => {
                        this.$refs.JNPFForm1.init(data)
                        this.flowVisible1 = false
                    })
                })
            },
			colseForm(isRefresh) {
                this.formVisible1 = false
                if (isRefresh) this.refresh()
            },
			getFlowEngineList() {
                FlowEngineListAll().then((res) => {
                    this.flowEngineList = res.data.list
                    this.flowEngineListAll = JSON.parse(JSON.stringify(this.categoryList))
                    for (let i = 0; i < this.flowEngineListAll.length; i++) {
                        let child = this.flowEngineList.filter(o => this.flowEngineListAll[i].enCode === o.category)
                        let count = child.length
                        this.$set(this.flowEngineListAll[i], 'children', child)
                        this.$set(this.flowEngineListAll[i], 'count', count)
                    }
                    this.flowEngineListAll = this.flowEngineListAll.filter(o => o.children.length)
                })
            },
            getDictionaryData() {
                this.$store.dispatch('base/getDictionaryData', { sort: 'WorkFlowCategory' }).then((res) => {
                    this.categoryList = res
                    this.getFlowEngineList()
                })
            },
        }
    }
</script>